package com.webull.information.center.carwler.dao;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.webull.framework.util.UtilJdbc;
import com.webull.information.center.carwler.common.model.StockConfig;
import com.webull.information.center.carwler.common.model.StockTitle;
import com.webull.information.center.dao.JdbcHelper;

/**
 * 个股采集信息
 * 
 * @author shimingjun
 * @date 2016年6月27日 下午5:35:58
 * @version 1.0
 * @since JDK 1.8
 */
@Repository
public class StockConfigDao {
	private static final RowMapper<StockConfig> stockConfigRM = BeanPropertyRowMapper.newInstance(StockConfig.class);
	protected final Logger logger = LogManager.getLogger(getClass());

	private @Autowired JdbcTemplate jdbcTemplate;

	private @Autowired NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	public StockConfig getBySource(StockTitle title, String collectSource, String lang) {
		StockConfig stockConfig = UtilJdbc.queryForObject(jdbcTemplate, Sql.GET_BY_ID_SOURCE, stockConfigRM,
				title.getTickerId(), collectSource, lang);

		if (stockConfig == null) {// 没有就新增一条记录

			StockConfig new_ = new StockConfig();
			new_.setTickerId(title.getTickerId());
			new_.setCollectSource(collectSource);
			new_.setLanguage(lang);
			new_.setExchangeCode(title.getExchangeCode());
			new_.setSymbol(title.getSymbol());

			new_.setId(UtilJdbc.insertAndReturnIntKey(namedParameterJdbcTemplate, Sql.ADD_BY_SOURCE, new_));
			// namedParameterJdbcTemplate.update(Sql.ADD_BY_SOURCE,
			// JdbcHelper.getSqlParameterSource(new_));
			// keyHolder.getKey().intValue();
			stockConfig = new_;
		}
		return stockConfig;
	}

	public StockConfig getByTarget(Integer target, String collectSource, String lang) {
		StockConfig stockConfig = UtilJdbc.queryForObject(jdbcTemplate, Sql.GET_BY_ID_SOURCE, stockConfigRM, target,
				collectSource, lang);

		if (stockConfig == null) {// 没有就新增一条记录

			StockConfig new_ = new StockConfig();
			new_.setTickerId(target);
			new_.setCollectSource(collectSource);
			new_.setLanguage(lang);
			new_.setExchangeCode("");
			new_.setSymbol("");

			new_.setId(UtilJdbc.insertAndReturnIntKey(namedParameterJdbcTemplate, Sql.ADD_BY_SOURCE, new_));
			// namedParameterJdbcTemplate.update(Sql.ADD_BY_SOURCE,
			// JdbcHelper.getSqlParameterSource(new_));
			// keyHolder.getKey().intValue();
			stockConfig = new_;
		}
		return stockConfig;
	}

	public void updateById(StockConfig stockConfig) {
		// jdbcTemplate.update(Sql.UPDATE_BY_ID, stockConfig.getCollectNumber(),
		// stockConfig.getCollectTime(), stockConfig.getTickerId(),
		// stockConfig.getCollectSoucre());
		namedParameterJdbcTemplate.update(Sql.UPDATE_BY_ID, JdbcHelper.getSqlParameterSource(stockConfig));
	}

	private class Sql {
		public static final String GET_BY_ID_SOURCE = "select * from wli_stock_config t where del = 0 and ticker_id = ? and collect_source = ? and language =?";
		public static final String ADD_BY_SOURCE = "INSERT into wli_stock_config (ticker_id,symbol, exchange_code,collect_source,language) VALUEs	 (:tickerId,:symbol,:exchangeCode,:collectSource,:language)";
		public static final String UPDATE_BY_ID = "UPDATE wli_stock_config  set  collect_number = :collectNumber, collect_time=:collectTime where ticker_id = :tickerId and collect_source= :collectSource and language=:language";
	}

}
